package com.wangmei.controller;

import com.wangmei.model.Productkuid;
import com.wangmei.pojo.ProductkuidPojo;
import com.wangmei.service.ProductkuidService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import unitTool.JsonRsp;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/productkuid")
public class ProductkuidController {
    private final Logger logger = LoggerFactory.getLogger(ProductkuidController.class);
    @Autowired
    private ProductkuidService productkuidService;

    /**
     * 根据pid查询某个商品的库存
     * @param pid
     * @return
     */
    @RequestMapping(value = "/getProductkuidAndAttributevalue",method = RequestMethod.GET)
    public JsonRsp getProductkuidAndAttributevalue(@RequestParam("pid")int pid){
        List<ProductkuidPojo> productkuidPojoList = productkuidService.getProductkuidPojo(pid);
        return JsonRsp.success(productkuidPojoList);
    }

    /**
     *修改多个商品的库存
     */
    @RequestMapping(value = "updateProductkuidList",method = RequestMethod.POST)
    public JsonRsp updateProductkuidList(@RequestBody Productkuid[] productkuidList){
        List<Productkuid> list = new ArrayList<>();
        logger.info("here");
        for (Productkuid pk : productkuidList){
            list.add(pk);
        }
        logger.info(String.valueOf(list.size()));
       int i = productkuidService.updateProductkuidList(list);
        if (i > 0){
            return JsonRsp.success("","success");
        }else{
            return JsonRsp.fail(400,"fail");
        }
    }
    /**
     * 修改单个商品的库存
     */
    @RequestMapping(value = "/updateProductkuid")
    public JsonRsp updateProductkuid(Productkuid productkuid){
        if (productkuidService.updateProductkuid(productkuid)>0){
            return JsonRsp.success("","success");
        }else{
            return JsonRsp.fail(400,"fail");
        }
    }

    /**
     * 删除某个商品的库存
     */
    @RequestMapping("/delProductkuid")
    public JsonRsp deleteProductkuid(@RequestParam("kuid")int kuid){
        if (productkuidService.deleteProductkuid(kuid)>0){
            return JsonRsp.success("","success");
        }else{
            return JsonRsp.fail(400,"fail");
        }
    }

    /**
     * 添加商品库存
     * @param productkuid
     * @return
     */
    @RequestMapping("/addProductkuid")
    public JsonRsp addProductkuid(@RequestBody Productkuid productkuid){
        if (productkuidService.addProductkuid(productkuid)>0){
            return JsonRsp.success("","success");
        }else{
            return JsonRsp.fail(400,"fail");
        }
    }
}
